/*! oauthapp.table 1.9.8 */
(function () {
    "use strict";

    var server = oauthapp.settings.server + '/api/AppStorage';

    var appid = oauthapp.settings.appid;

    var bearerToken = "Bearer " + oauthapp.settings.access_token;

    /**
     * 数据表集合
     * */
    oauthapp.tables = function () {
        var settings = {
            "url": server + "/" + appid + "/Tables",
            "method": "GET",
            "headers": {
                "Content-Type": "application/json",
                'Authorization': bearerToken
            }
        };

        return $.ajax(settings);
    }


    /**
     * 查询数据
     * @param {any} _tableName 数据表名称
     * @param {any} _filter 查询条件
     * @param {any} _sort 排序
     * @param {any} _take 每页条数
     * @param {any} _skip 跳过多少条数据
     */
    oauthapp.table = function (_tableName, _filter = '', _sort = '', _take = "10", _skip = "0") {

        var queryParams = [
            "tName=" + _tableName,
            "filter=" + _filter,
            "sort=" + _sort,
            "take=" + _take,
            "skip=" + _skip
        ]

        var settings = {
            "url": server + "/" + appid + "/Query?" + queryParams.join("&"),
            "method": "GET",
            "headers": {
                "Content-Type": "application/json",
                'Authorization': bearerToken
            }
        };

        return $.ajax(settings);
    }

    /**
     * 添加数据
     * @param {any} _tableName 数据表名称
     * @param {any} _jsonStr json字符串
     */
    oauthapp.tablePost = function (_tableName, _jsonStr = '') {

        var queryParams = [
            "tName=" + _tableName
        ];

        var settings = {
            "url": server + "/" + appid + "/Post?" + queryParams.join("&"),
            "method": "POST",
            "headers": {
                "Content-Type": "application/json",
                'Authorization': bearerToken
            },
            data: _jsonStr,
        };

        return $.ajax(settings);
    }

    /**
     * 删除数据
     * @param {any} _id id
     */
    oauthapp.tableDelete = function (_id) {

        var queryParams = [
            "id=" + _id
        ];

        var settings = {
            "url": server + "/" + appid + "/Delete?" + queryParams.join("&"),
            "method": "DELETE",
            "headers": {
                "Content-Type": "application/json",
                'Authorization': bearerToken
            },
        };

        return $.ajax(settings);
    }

    /**
     * 更新数据
     * @param {any} _id id
     * @param {any} _jsonStr json字符串
     */
    oauthapp.tablePut = function (_id, _jsonStr = '') {

        var queryParams = [
            "id=" + _id
        ];

        var settings = {
            "url": server + "/" + appid + "/Put?" + queryParams.join("&"),
            "method": "PUT",
            "headers": {
                "Content-Type": "application/json",
                'Authorization': bearerToken
            },
            "data": _jsonStr,
        };

        return $.ajax(settings);
    }


})(window);